package com.atguigu.yygh.cmn.controller;


import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author xin Zhou
 * @since 2024-01-16
 */
@RestController
@RequestMapping("/admin/cmn")
public class DictController {

    @Autowired
    private DictService dictService;

//    根据医院的省市区编号获取该省的省市区
    @ApiOperation("根据医院的省市区编号获取该省的省市区")
    @GetMapping("/{code}")
    public String getNameByValue(@PathVariable("code") Long value){
        return  dictService.getNameByValue(value);
    }



    @ApiOperation(value = "根据医院的等级编号获取医院的等级信息")
    @GetMapping(value = "/{parentDictCode}/{value}")
    public String getName(
            @ApiParam(name = "parentDictCode", value = "上级编码", required = true)
            @PathVariable("parentDictCode") String parentDictCode,
            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue(parentDictCode, value);
    }

//    /admin/cmn/childList/{pid}

    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public R findByDictCode(
            @ApiParam(name = "dictCode", value = "节点编码", required = true)
            @PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return R.ok().data("list",list);
    }


//    形参要与前端接口的name属性名称保持一致
    @ApiOperation("更新数据")
    @PostMapping("/imporData")
    public R imporData(MultipartFile file) throws IOException {
        dictService.upload(file);
        return R.ok();
    }

    @ApiOperation("下载数据")
    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
         dictService.downlaod(response);
    }

//    /admin/cmn/childList/{pid}
    @ApiOperation("根据parentId查询子对象")
    @GetMapping("/childList/{pid}")
    public R getChildListByPid(@PathVariable Long pid){
        List<Dict> list = dictService.getChildListByPid(pid);
        return R.ok().data("items",list);
    }


}

